package com.cskaoyan.mall.product.service.impl;

import com.cskaoyan.mall.product.converter.dto.PlatformAttributeInfoConverter;
import com.cskaoyan.mall.product.dto.PlatformAttributeInfoDTO;
import com.cskaoyan.mall.product.dto.PlatformAttributeValueDTO;
import com.cskaoyan.mall.product.mapper.PlatformAttrInfoMapper;
import com.cskaoyan.mall.product.mapper.PlatformAttrValueMapper;
import com.cskaoyan.mall.product.model.PlatformAttributeInfo;
import com.cskaoyan.mall.product.model.PlatformAttributeValue;
import com.cskaoyan.mall.product.query.PlatformAttributeParam;
import com.cskaoyan.mall.product.query.PlatformAttributeValueParam;
import com.cskaoyan.mall.product.service.PlatformAttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PlatformAttributeServiceImpl implements PlatformAttributeService {
    @Autowired
    PlatformAttrInfoMapper platformAttrInfoMapper;
    @Autowired
    PlatformAttrValueMapper platformAttrValueMapper;
    @Autowired
    PlatformAttributeInfoConverter platformAttributeInfoConverter;
    @Override
    public List<PlatformAttributeInfoDTO> getPlatformAttrInfoList(Long firstLevelCategoryId, Long secondLevelCategoryId, Long thirdLevelCategoryId) {
        //这个地方要写个连表查询
        //好复杂啊，，表1连表2连表3
        List<PlatformAttributeInfo> platformAttributeInfos = platformAttrInfoMapper.selectPlatFormAttrInfoList(firstLevelCategoryId, secondLevelCategoryId, thirdLevelCategoryId);
        List<PlatformAttributeInfoDTO> platformAttributeInfoDTOS = platformAttributeInfoConverter.platformAttributeInfoPOs2DTOs(platformAttributeInfos);
        return platformAttributeInfoDTOS;
    }

    @Override
    @Transactional
    public void savePlatformAttrInfo(PlatformAttributeParam platformAttributeParam) {
        //这里写保存和修改
        //先写保存

        //info表内容
        Date current=new Date();
        PlatformAttributeInfo platformAttributeInfo=PlatformAttributeInfo.builder()
                .attrName(platformAttributeParam.getAttrName())
                .categoryId(platformAttributeParam.getCategoryId())
                .categoryLevel(platformAttributeParam.getCategoryLevel())
                .build();
        platformAttributeInfo.setCreateTime(current);
        platformAttributeInfo.setUpdateTime(current);
        platformAttributeInfo.setIsDeleted(0);
        if (platformAttributeParam.getId()==null){
            platformAttrInfoMapper.insert(platformAttributeInfo);
        }else {
            platformAttributeInfo.setCreateTime(null);
            platformAttributeInfo.setId(platformAttributeParam.getId());
            platformAttrInfoMapper.updateById(platformAttributeInfo);

            //这里把对应的修改操作也写了
            Map<String,Object> delMap=new HashMap<>();
            delMap.put("attr_id",platformAttributeParam.getId());
            platformAttrValueMapper.deleteByMap(delMap);
        }

        //value表内容，修改相关内容已经被删除了
        for (PlatformAttributeValueParam attrValue : platformAttributeParam.getAttrValueList()) {
            PlatformAttributeValue platformAttributeValue=PlatformAttributeValue.builder()
                    .valueName(attrValue.getValueName())
                    .attrId(platformAttributeInfo.getId())
                    .build();
            platformAttributeInfo.setCreateTime(current);
            platformAttributeInfo.setUpdateTime(current);
            platformAttributeInfo.setIsDeleted(0);
            platformAttrValueMapper.insert(platformAttributeValue);
        }



    }

    @Override
    public List<PlatformAttributeValueDTO> getPlatformAttrInfoList(Long attrId) {
        Map<String ,Object> map=new HashMap<>();
        map.put("attr_id",attrId);
        List<PlatformAttributeValue> platformAttributeValues = platformAttrValueMapper.selectByMap(map);
        List<PlatformAttributeValueDTO> platformAttributeValueDTOS = platformAttributeInfoConverter.platformAttributeValuePOs2DTOs(platformAttributeValues);

        return platformAttributeValueDTOS;
    }
}
